package com.dsl.dslallinone.core;

import com.dsl.dslallinone.common.exception.DslException;
import com.dsl.dslallinone.dto.Granularity;
import org.springframework.stereotype.Component;

/**
 * 时间桶SQL生成器
 * 统一生成不同粒度的时间桶表达式
 */
@Component
public class TimeBucketSql {

    /**
     * 构建时间桶SQL表达式
     */
    public String buildTimeBucket(Granularity granularity) {
        switch (granularity) {
            case HOUR:
                return buildHourTimeBucket();
            case DAY:
                return buildDayTimeBucket();
            case MONTH:
                return buildMonthTimeBucket();
            default:
                throw new DslException("不支持的时间粒度: " + granularity);
        }
    }

    /**
     * 构建小时时间桶
     * 格式: 2025-08-01 14:00:00
     */
    private String buildHourTimeBucket() {
        return "DATE_FORMAT(stat_date, '%Y-%m-%d %H:00:00')";
    }

    /**
     * 构建日时间桶
     * 格式: 2025-08-01
     */
    private String buildDayTimeBucket() {
        return "DATE_FORMAT(stat_date, '%Y-%m-%d')";
    }

    /**
     * 构建月时间桶
     * 格式: 2025-08
     */
    private String buildMonthTimeBucket() {
        return "DATE_FORMAT(stat_date, '%Y-%m')";
    }

    /**
     * 构建同比时间桶（去年同期）
     */
    public String buildYoyTimeBucket(Granularity granularity) {
        switch (granularity) {
            case HOUR:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 YEAR), '%Y-%m-%d %H:00:00')";
            case DAY:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 YEAR), '%Y-%m-%d')";
            case MONTH:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 YEAR), '%Y-%m')";
            default:
                throw new DslException("不支持的时间粒度: " + granularity);
        }
    }

    /**
     * 构建环比时间桶（上期）
     */
    public String buildMomTimeBucket(Granularity granularity) {
        switch (granularity) {
            case HOUR:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 HOUR), '%Y-%m-%d %H:00:00')";
            case DAY:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 DAY), '%Y-%m-%d')";
            case MONTH:
                return "DATE_FORMAT(DATE_SUB(stat_date, INTERVAL 1 MONTH), '%Y-%m')";
            default:
                throw new DslException("不支持的时间粒度: " + granularity);
        }
    }

    /**
     * 构建时间范围条件
     */
    public String buildTimeRangeCondition(String startDate, String endDate, Granularity granularity) {
        switch (granularity) {
            case HOUR:
                return String.format("stat_date >= '%s 00:00:00' AND stat_date <= '%s 23:59:59'", startDate, endDate);
            case DAY:
                return String.format("stat_date >= '%s' AND stat_date <= '%s'", startDate, endDate);
            case MONTH:
                return String.format("DATE_FORMAT(stat_date, '%%Y-%%m') >= '%s' AND DATE_FORMAT(stat_date, '%%Y-%%m') <= '%s'", 
                    startDate.substring(0, 7), endDate.substring(0, 7));
            default:
                throw new DslException("不支持的时间粒度: " + granularity);
        }
    }

    /**
     * 获取时间字段名
     */
    public String getTimeField(Granularity granularity) {
        switch (granularity) {
            case HOUR:
                return "stat_hour";
            case DAY:
                return "stat_date";
            case MONTH:
                return "stat_month";
            default:
                return "stat_date";
        }
    }
}
